#include <interface\abstractTextInput.h>
#include <interface\window.h>

abstractTextInput::abstractTextInput(): winElmt(),
                                        content(""),
                                        cursor(0)
{
    ELname = "<abstractTextInput>";
}
abstractTextInput::abstractTextInput(window* win):  winElmt(win),
                                                    content(""),
                                                    cursor(0)
{
    ELname = "<abstractTextInput>";
}
abstractTextInput::~abstractTextInput(){}

void abstractTextInput::setCursor(int pos){
    cursor = pos;
}
void abstractTextInput::setContent(string instr){
    content = instr;
    cursor = content.size();
}
string abstractTextInput::getContent(){
    return content;
}
int abstractTextInput::getCursor(){
    return cursor;
}

        unsigned char abstractTextInput::eventExec(unsigned char in){
    if( !parentWin ) return in;
    in = winElmt::eventExec(in);
    if( in & ACTIVE_TAKEN ){
        parentWin->getInput()->setLastTyped(0);
        parentWin->getInput()->setKey(VK_BACK,0);
        parentWin->getInput()->setKey(VK_LEFT,0);
        parentWin->getInput()->setKey(VK_RIGHT,0);
        parentWin->getInput()->setKey(VK_END,0);
        parentWin->getInput()->setKey(VK_HOME,0);
        parentWin->getInput()->setKey(VK_CONTROL,0);
        parentWin->getInput()->setKey(VK_DELETE,0);
        parentWin->getInput()->setKey(VK_RETURN,0);
        parentWin->getInput()->setKey(0x56,0); //V
    }else if( active ){
        char stack;
        if( parentWin->getInput()->bindKey(VK_BACK) ){
            if( cursor != 0 ){
                content.erase( cursor-1, 1);
                cursor -= 1;
            }
            parentWin->getInput()->setLastTyped(0);
        }
        if( parentWin->getInput()->bindKey(VK_DELETE)){
            content.erase(cursor,1);
        }
        if( parentWin->getInput()->bindKey(VK_RETURN)){
           parentWin->eventListener(new actionEnterEvent(this));
        }
        if( (stack = parentWin->getInput()->bindLastTyped()) ){
            switch( stack ){
                default:    if( !parentWin->getInput()->getInserMode() ) content.replace(cursor,1,1,stack);
                            else content.insert(cursor,1,stack);
                            cursor++;
                            break;
                case '\t':
                case '\n':
                case '\r': break;
            }
        }
        if( parentWin->getInput()->bindKey(VK_LEFT) ){
            if( cursor != 0 ) cursor--;
        }
        if( parentWin->getInput()->bindKey(VK_RIGHT) ){
            cursor++;
            if( cursor > content.size() ) cursor = content.size();
        }
        if( parentWin->getInput()->bindKey(VK_END) ){
            cursor = content.size();
        }
        if( parentWin->getInput()->bindKey(VK_HOME) ){
            cursor = 0;
        }
        if( parentWin->getInput()->getKey(VK_CONTROL) && parentWin->getInput()->getKey(0x56)){
            parentWin->getInput()->setKey(VK_CONTROL,0);
            parentWin->getInput()->setKey(0x56,0);
            if( IsClipboardFormatAvailable(CF_TEXT) ){
                if( OpenClipboard(NULL) ){
                    string pasteData;
                    pasteData.assign( (char*)GetClipboardData(CF_TEXT) );
                    unsigned int found;
                    while((found = pasteData.find('\n'))!=string::npos){
                        pasteData.erase(found);
                    }
                    if( parentWin->getInput()->getInserMode() ){
                        content.insert(cursor,pasteData);
                    }else{
                        content.replace(cursor,pasteData.size(),pasteData);
                    }
                    cursor += pasteData.size();
                    CloseClipboard();
                }
            }
        }
    }
    return in;
}

actionEnterEvent::actionEnterEvent(winElmt* e):   event( e )
{
    type = event_type_action_enter;
}
